package cs422.group1.projectsquirrel;

import cs422.group1.projectsquirrel.ExtraSurvey.myOnItemSelectedListener;
import cs422.group1.projectsquirrel.ExtraSurvey.nextQuestionListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;


public class ExtraSurvey3 extends Activity
{
	//Instance variables.
	private Button nextButton;
	private Spinner birdSpinner;
	private Spinner humanSpinner;
	private Spinner garbageSpinner;
	private Spinner treesPlantsSpinner;
	private EditText other;
	
	private JavaSucks birdValue, humanValue, garbageValue, treeValue;
	
	private String valueOfSpinner;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.extrasurveypage3);
		
		birdValue = new JavaSucks("");
		humanValue = new JavaSucks("");
		garbageValue = new JavaSucks("");
		treeValue = new JavaSucks("");
		
		nextButton = (Button)findViewById(R.id.nextButton3);
		birdSpinner = (Spinner)findViewById(R.id.birdSpinner);
		humanSpinner = (Spinner)findViewById(R.id.humanSpinner);
		garbageSpinner = (Spinner)findViewById(R.id.garbageSpinner);
		treesPlantsSpinner = (Spinner)findViewById(R.id.treesPlantsSpinner);
		other = (EditText)findViewById(R.id.txtOther);
		
		//Bird Spinner adapter
        ArrayAdapter<CharSequence> birdAdapter = ArrayAdapter.createFromResource(
        		this, R.array.NeverAlways, R.layout.spinner_style);
        birdAdapter.setDropDownViewResource(R.layout.combo_style);
        birdSpinner.setAdapter(birdAdapter);
        birdSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(birdValue));
        
        
        //Human Adapter
        ArrayAdapter<CharSequence> humanAdapter = ArrayAdapter.createFromResource(
        		this, R.array.NeverAlways, R.layout.spinner_style);
        humanAdapter.setDropDownViewResource(R.layout.combo_style);
        humanSpinner.setAdapter(humanAdapter);
        humanSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(humanValue));
        
        //Garbage adapter
        ArrayAdapter<CharSequence> garbageAdapter = ArrayAdapter.createFromResource(
        		this, R.array.NeverAlways, R.layout.spinner_style);
        garbageAdapter.setDropDownViewResource(R.layout.combo_style);
        garbageSpinner.setAdapter(garbageAdapter);
        garbageSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(garbageValue));
        
        //Trees/Plants Spinner
        ArrayAdapter<CharSequence> treesPlantsAdapter = ArrayAdapter.createFromResource(
        		this, R.array.NeverAlways, R.layout.spinner_style);
        treesPlantsAdapter.setDropDownViewResource(R.layout.combo_style);
        treesPlantsSpinner.setAdapter(treesPlantsAdapter);
        treesPlantsSpinner.setOnItemSelectedListener(new myOnItemSelectedListener(treeValue));        
        
        //Attaching a listener for callback on the next button.
        nextButton.setOnClickListener(new nextQuestionListener()); 
        
        if (ObservationDataManager.isEdit())
        {
        	birdSpinner.setSelection(birdAdapter.getPosition(ObservationDataManager.getData().feed_bird_feeder));
        	humanSpinner.setSelection(humanAdapter.getPosition(ObservationDataManager.getData().feed_handouts));
        	garbageSpinner.setSelection(garbageAdapter.getPosition(ObservationDataManager.getData().feed_garbage));
        	treesPlantsSpinner.setSelection(treesPlantsAdapter.getPosition(ObservationDataManager.getData().feed_trees));
        	other.setText(ObservationDataManager.getData().feed_other);
        }
	}
	
	//Callback function for the drop down box on the current page.
	public class myOnItemSelectedListener implements OnItemSelectedListener
	{
		JavaSucks val;
		public myOnItemSelectedListener(JavaSucks _val)
		{
			val = _val;
		}
		public void onItemSelected(AdapterView<?> parent,View view,int pos,long id)
		{
			  val.setValue(parent.getItemAtPosition(pos).toString());
		}
		public void onNothingSelected(AdapterView<?> parent)
		{
			//Do Nothing!
		}
	}
	private void gatherData()
	{
		ObservationData od = ObservationDataManager.getData();
		od.feed_bird_feeder = birdValue.getValue();
		od.feed_handouts = humanValue.getValue();
		od.feed_garbage = garbageValue.getValue();
		od.feed_trees = treeValue.getValue(); 
		od.feed_other = other.getText().toString();
		
	}
	
	//Callback function for the NEXT button.
	public class nextQuestionListener implements OnClickListener
	{
		public void onClick(View view)
		{
			gatherData();
			nextPage();
		}
	}
		
	
	//Done with this activity, and moving on to the next one.
	private void nextPage()
	{
		Intent intent = new Intent(this.getBaseContext(),ExtraSurvey4.class);
		startActivity(intent);
	}

	
}
